package jxc.data.hibernate.dao;

import java.util.List;
import org.hibernate.Query;
import jxc.data.hibernate.pojo.Inport;

public class InportDAO extends HibernateBaseDAO<Inport> {
	
	public Inport getById(String id){
		String hql="from Inport where id=:id";
		Query query=session.createQuery(hql);
		query.setString("id", id);
		return (Inport) query.uniqueResult();
	}
	
	public Inport getByGoodsProvider(String gid,String pid){
		String hql="from Inport where tbProvider.id=:pid and tbGoods.id=:gid";
		Query query=session.createQuery(hql);
		query.setString("gid", gid);
		query.setString("pid", pid);
		return (Inport) query.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	public List<Inport> getLikeGoodsnameByProvider(String name,String pid){
		String hql="from Inport where tbProvider.id=:pid and tbGoods.goodsname like :name";
		Query query=session.createQuery(hql);
		query.setString("name", '%'+name+'%');
		query.setString("pid", pid);
		return query.list();
	}
	
	public long countGoods(String gid){
		String hql="select sum(number) from Inport where tbGoods.id=:id";
		Query query=session.createQuery(hql);
		query.setString("id", gid);
		Long number=(Long)query.uniqueResult();
		if(number==null)
			return 0;
		return number;
	}
}
